import 'package:Smart/utils/gs_dialog_util.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

/// 平台权限检测并申请设置工具类
///
class GSPermissionUtil {
  /// 检测并申请定位权限
  static Future<bool> requestLocationPermission({BuildContext context}) async {
    //获取当前的权限
    var status = await Permission.location.status;
    if (status == PermissionStatus.granted) {
      return true;
    } else {
      status = await Permission.location.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        if (context != null) {
          GSDialogUtil.showConfirm(context,
              imagePath: "resources/images/amaps/map_pop_location.png",
              message: "您没有开启定位服务",
              knowText: "去设置", onConfirm: (value) {
            openAppSettings();
          });
        }
        return false;
      }
    }
  }

  /// 检测并申请相册权限
  static Future<bool> requestPhotosPermission({BuildContext context}) async {
    var status = await Permission.photos.status;
    if (status == PermissionStatus.granted) {
      return true;
    } else {
      status = await Permission.photos.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        if (context != null) {
          GSDialogUtil.showConfirm(context,
              message: "您没有授权相册权限", knowText: "去设置", onConfirm: (value) {
            openAppSettings();
          });
        }
        return false;
      }
    }
  }
}
